/**
 * Created by Administrator on 2017/1/6.
 */

var UserToken = module.exports;
var redis_db = require('./config/redis2userToken.js');
var commonUtil = require('./util/common.js');

function generateRandom(uid){
    return commonUtil.randomString(12) + '_' + uid;
};

UserToken.generateRandom = generateRandom;

UserToken.onLogin = function (userInfo, cb) {
    var account ;
    var time;
    if(userInfo.isAdmin) {
        account = 'adm_'+userInfo.account;
        time = 1800;
    } else {
        account = 'user_'+userInfo.account;
        time = 48*3600;
    }

    return redis_db.get(account, function(err, result) {
        var token;
        if(err) {
            console.log(err);
            cb(err);
        }
        token = result;
        if(!token){
            if(userInfo.isAdmin) {
                var adm_uid = userInfo.uid + 'yyt';
                token = generateRandom(adm_uid);
                var str = {};
                str.time = time;
                str.uid = userInfo.uid;
                // str.purview_Id = userInfo.purview_Id;
                str.isAdmin = userInfo.isAdmin;
                str.account = userInfo.account;
                str.role = userInfo.role;
                str = JSON.stringify(str);
                redis_db.set(token, str, time, function(err){
                    if(err) {
                        console.log(err);
                        return;
                    }
                    console.log('admin_token insert redis');
                });
            } else {
                token = generateRandom(userInfo.uid);
                var str = {};
                str.time = time;
                str.uid = userInfo.uid;
                str.account = userInfo.account;
                str = JSON.stringify(str);
                redis_db.set(token, str, time, function(err){
                    if(err) {
                        console.log(err);
                        return;
                    }
                    console.log('user_token insert redis');
                });
            }
            redis_db.set(account ,token ,time, function (err) {
                if(err) {
                    console.log(err);
                    cb(err);
                }
                console.log('token2info insert success');
            });
            cb(null, token);
            return;
        }
        redis_db.expire(account, time, function(err){
            if(err) {
                console.log(err);
                cb(err);
            }

        });
        redis_db.expire(token, time, function(err){
            if(err) {
                console.log(err);
                cb(err);
            }
        });
        cb(null, token);
        return;
    });
};

UserToken.checkLogin = function (token, cb) {
    if(token == undefined) {
        cb(null, false)
    } else {
        redis_db.get(token, function(err, result){
            if(err) {
                return;
            }
            if(result) {
                result = JSON.parse(result);
                cb(null, result);
                var account ;
                var time ;
                if(result.isAdmin) {
                    account = 'adm_'+result.account;
                    time = 1800;
                } else {
                    account = 'user_'+result.account;
                    time = 48*3600;
                }
                redis_db.expire(token, time, function(err){
                    if(err) {
                        console.log(err);
                        //cb(err);
                    }
                });
                redis_db.expire(account, time, function(err){
                    if(err) {
                        console.log(err);
                        //cb(err);
                    }
                });
            } else {
                redis_db.del(token, function (err) {
                    if(err) {
                        console.log(err);
                    }
                });
                cb(null, false);
            }
        });
    }
};

UserToken.onLogout = function (token ,cb) {
    return redis_db.get(token, function(err, result){
        if(err) {
            return;
        }
        if(result) {
            result = JSON.parse(result);
            var account ;
            if(result.isAdmin) {
                account = 'adm_'+result.account;
            } else {
                account = 'user_'+result.account;
            }
            redis_db.del(account, function(err){
                if(err) {
                    return;
                }
                cb(null ,true);
            });
            redis_db.del(token, function (err) {
                if(err) {
                    return;
                }
            });
        } else {
            cb(null ,false);
        }
    });
};


